package com.cmlite.adapter;

import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.cmlite.R;
import com.cmlite.model.PeopleModel;

public class PeopleAdapter extends ArrayAdapter<PeopleModel> implements
		SectionIndexer {

	private LayoutInflater inflater;
	private List<PeopleModel> mArr;
	private int layout;
	private Context mContext;
	private Resources mRes;
	private OnClickListListener listenerClick;
	private OnCBClickListListener listenerCheckBoxClick;
	private OnLongClickListListener listenerLongClick;

	public PeopleAdapter(Context context, int textViewResourceId,
			List<PeopleModel> objects, OnClickListListener listenerClick,
			OnCBClickListListener listenerCheckBoxClick,
			OnLongClickListListener listenerLongClick) {
		super(context, textViewResourceId);
		// TODO Auto-generated constructor stub

		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.layout = textViewResourceId;
		this.mArr = objects;
		this.mContext = context;
		this.mRes = context.getResources();
		this.listenerClick = listenerClick;
		this.listenerCheckBoxClick = listenerCheckBoxClick;
		this.listenerLongClick = listenerLongClick;
	}

	@Override
	public int getCount() {
		return mArr.size();
	}

	@Override
	public PeopleModel getItem(int position) {
		return mArr.get(position);
	}

	@Override
	public View getView(final int position, View convertView,
			final ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = convertView;
		final ViewHolder mHolder;
		if (view == null) {
			view = inflater.inflate(layout, null);
			mHolder = new ViewHolder();
			mHolder.tv_type = (TextView) view
					.findViewById(R.id.item_people_tv_type);
			mHolder.layout_data = (LinearLayout) view
					.findViewById(R.id.item_people_layout);
			mHolder.tv_firtname = (TextView) view
					.findViewById(R.id.item_people_tv_firstname);
			mHolder.tv_lastname = (TextView) view
					.findViewById(R.id.item_people_tv_lastname);
			mHolder.tv_field_01 = (TextView) view
					.findViewById(R.id.item_people_tv_field01);
			mHolder.tv_field_02 = (TextView) view
					.findViewById(R.id.item_people_tv_field02);
			mHolder.tv_field_03 = (TextView) view
					.findViewById(R.id.item_people_tv_field03);
			mHolder.cb_checbox = (TextView) view
					.findViewById(R.id.item_people_cb_checkbox);
			view.setTag(mHolder);
		} else {
			mHolder = (ViewHolder) view.getTag();
		}

		if (mArr.size() > 0) {
			final PeopleModel mPeople = mArr.get(position);
			if (!mPeople.isGroup()) {
				mHolder.changeLayout(1);

				if (mPeople.isColor())
					mHolder.layout_data
							.setBackgroundResource(R.xml.item_selector_listview_01);
				else
					mHolder.layout_data
							.setBackgroundResource(R.xml.item_selector_listview_02);

				mHolder.tv_firtname.setText(mPeople.getmField_01());
				mHolder.tv_lastname.setText(mPeople.getmField_02());
				mHolder.tv_field_01.setText(mPeople.getmField_03());
				mHolder.tv_field_02.setText(mPeople.getmField_04());
				mHolder.tv_field_03.setText(mPeople.getmField_05());

				if (mPeople.isCheck())
					mHolder.cb_checbox
							.setBackgroundResource(R.drawable.item_sel);
				else
					mHolder.cb_checbox
							.setBackgroundResource(R.drawable.item_unsel);

				mHolder.cb_checbox.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (!mPeople.isGroup()) {
							if (mPeople.isCheck())
								mHolder.cb_checbox
										.setBackgroundResource(R.drawable.item_unsel);
							else
								mHolder.cb_checbox
										.setBackgroundResource(R.drawable.item_sel);
							listenerCheckBoxClick.onClick(mPeople);
							PeopleAdapter.this.notifyDataSetChanged();
						}
					}
				});
				mHolder.layout_data.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						if (!mPeople.isGroup()) {
							listenerClick.onClick(mPeople);
							PeopleAdapter.this.notifyDataSetChanged();
						}
					}
				});
				mHolder.layout_data
						.setOnLongClickListener(new OnLongClickListener() {

							@Override
							public boolean onLongClick(View v) {
								// TODO Auto-generated method stub
								if (!mPeople.isGroup()) {
									for(PeopleModel m : mArr) {
										if (m.isSelected()) {
											m.setSelected(false);
										}
									}
									mPeople.setSelected(true);
									listenerLongClick.onLongClick(v, mPeople,
											position);
									return true;
								}
								return false;
							}
						});
				if (mPeople.isSelected()) {
					mHolder.layout_data.setSelected(true);
				} else {
					mHolder.layout_data.setSelected(false);
				}
			} else {
				mHolder.changeLayout(0);
				mHolder.tv_type.setText(mPeople.getmField_01());
			}

		}

		return view;
	}

	class ViewHolder {
		TextView tv_type, tv_firtname, tv_lastname, tv_field_01, tv_field_02,
				tv_field_03, cb_checbox;
		LinearLayout layout_data;

		public void changeLayout(int i) {
			if (i == 0) { // type
				layout_data.setVisibility(View.GONE);
				tv_type.setVisibility(View.VISIBLE);
			} else if (i == 1) { // data
				layout_data.setVisibility(View.VISIBLE);
				tv_type.setVisibility(View.GONE);
			}
		}
	}

	@Override
	public int getPositionForSection(int section) {
		try {
			if (mArr != null || mArr.size() > 0) {
				if (section == 35) { // 35
					return 0;
				}
				for (int i = 0; i < mArr.size(); i++) {
					if (mArr.get(i).getmField_01() != null
							&& mArr.get(i).getmField_01().length() > 0) {
						String l = mArr.get(i).getmField_01();
						char firstChar = l.toUpperCase().charAt(0);
						if (firstChar == section) {
							return i;
						}
					}

				}
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}

		return -1;
	}

	@Override
	public int getSectionForPosition(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object[] getSections() {
		// TODO Auto-generated method stub
		return null;
	}

	public static interface OnCBClickListListener {
		public abstract void onClick(PeopleModel data);
	}

	public static interface OnClickListListener {
		public abstract void onClick(PeopleModel data);
	}

	public static interface OnLongClickListListener {
		public abstract void onLongClick(View v, PeopleModel data, int position);
	}
}
